package com.achobeta.recruitment.service.impl.interact;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.config.AppConfig;
import com.achobeta.recruitment.dao.interact.AttachmentMapper;
import com.achobeta.recruitment.model.*;
import com.achobeta.recruitment.service.interact.AttachmentService;
import com.achobeta.recruitment.service.wx.WxUserService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.MediaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-30
 * Time: 21:26
 */
@Service
@Slf4j
public class AttachmentServiceImpl implements AttachmentService {


    @Value("${redirect.domain}")
    private String redirectDomain;


    @Autowired
    private AttachmentMapper attachmentMapper;

    @Autowired
    private WxUserService wxUserService;

    @Override
    public void loadFile(MultipartFile file, String path, String openid) {
        // 校验
        CheckUtils.checkNULL(file, path);
        // 下载
        MediaUtils.loadFile(file, AppConfig.ROOT + path);
        log.info(ResultCode.SUCCESS + "保存成功：" + redirectDomain + "/" + path);
    }


    @Override
    public void insertAttachment(Attachment attachment) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, attachment.getAttachment(),
                attachment.getRemark(), attachment.getOpenid());
        // 插入
        int rows = attachmentMapper.insertSelective(attachment);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 成功
        log.info(ResultCode.SUCCESS + "附件id：" + attachment.getId());
    }

    @Override
    public void deleteAttachment(Attachment attachment) {
        // 校验
        CheckUtils.checkNULL(attachment, attachment.getId(), attachment.getOpenid());
        // 查看这条信息是否是
        Attachment attach = selectById(attachment.getId());
        if(attach == null || !attach.getOpenid().equals(attachment.getOpenid())) {
            CheckUtils.printWarn(ResultCode.FAILED_UNAUTHORIZED);
        }
        // 开始删除
        int rows = attachmentMapper.deleteById(attachment.getId());
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED);
        // 删除成功，顺便把文件也删除了
        assert attach != null; //断言语法
        boolean flag = new File(AppConfig.ROOT + attach.getAttachment()).delete();
        if(flag == false) {
            CheckUtils.printWarn(ResultCode.FAILED);
        }
        // 成功
        log.info(ResultCode.SUCCESS + " 已删除附件id：" + attachment.getId());
    }


    @Override
    public List<Attachment> selectByOpenId(String openid) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 判断此用户是否存在
        WxUser wxUser = wxUserService.selectWxUserByOpenid(openid);
        CheckUtils.checkNULL(wxUser);
        // 查询
        List<Attachment> list = attachmentMapper.selectByOpenId(openid);
        list = list == null ? new ArrayList<>() : list;
        // 无需校验，返回即可
        log.info(ResultCode.SUCCESS + "  查询到的行数： " + list.size());
        return list;
    }

    @Override
    public Attachment selectById(Integer id) {
        // 校验
        CheckUtils.checkNULL(id);
        // 查询
        Attachment attachment = attachmentMapper.selectByPrimaryKey(id);
        // 校验
        CheckUtils.checkNULL(attachment);
        // 成功返回
        log.info(ResultCode.SUCCESS + "  " + attachment);
        return attachment;
    }
}
